package com.mf.springboot.backend.userAuth.controller;

import com.github.pagehelper.PageInfo;
import com.mf.springboot.backend.commonModule.bean.Const;
import com.mf.springboot.backend.commonModule.bean.JSONResult;
import com.mf.springboot.backend.commonModule.bean.PageBean;
import com.mf.springboot.backend.commonModule.common.CryptographyUtil;
import com.mf.springboot.backend.commonModule.common.UUIDGeneratorUtil;
import com.mf.springboot.backend.mybatisModule.entity.TLoginUser;
import com.mf.springboot.backend.mybatisModule.entity.TLoginUserExample;
import com.mf.springboot.backend.mybatisModule.mapper.TLoginUserMapper;
import com.mf.springboot.backend.userAuth.service.LoginService;
import com.mf.springboot.backend.userAuth.service.LoginTestService;
import com.xiaoleilu.hutool.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by lx on 2017/9/12.
 */
@Controller
@RequestMapping("login")
public class LoginController {
    SimpleDateFormat matter=new SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat matter1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Resource
    private LoginService loginService;

    @Resource
    private LoginTestService loginTestService;

    @RequestMapping("/checkLogin")
    @ResponseBody
    public JSONResult checkLogin(String userName,String password ,String key) {
        JSONResult result = JSONResult.getSuccess();

        String times = matter.format(new Date());
        String message = "framework"+times;
        String md5Pwd = getEncryptionPwd(message);
        if(md5Pwd.equals(key)){
            try {
                TLoginUser loginUser = loginService.selectByUserName(userName);
                if (loginUser != null && loginUser.getPassword().equals(password)) {
                    String pwd = loginUser.getUserName()+new Date();
                    String encryptionPwd = getEncryptionPwd(pwd);
                    result.getBody().put("token", encryptionPwd);
                    result.getBody().put("loginUser",loginUser);
                }else {
                    return JSONResult.getFail("用户名或密码错误");
                }
                return result;
            } catch (Exception e) {
                e.printStackTrace();
                return JSONResult.getFail("系统异常！");
            }
        }else {
            return JSONResult.getFail("系统异常！");
        }

    }
    //获取加密后的密码(MD5)
    public String getEncryptionPwd(String pwd){
        try {
            return CryptographyUtil.md5(pwd, Const.MD5SALT);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    @RequestMapping("/test")
    @ResponseBody
    public List test() {
        List list = null;
        try {
//            TLoginUser tLoginUser = new TLoginUser();
//            tLoginUser.setId(UUIDGeneratorUtil.generate());
//            tLoginUser.setPassword("888888");
//            loginService.insertLoginUser(tLoginUser);
//
//            TLoginUserExample example = new TLoginUserExample();
//            list=loginService.selectByExample(example);

            list = loginService.test();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @RequestMapping("/userList")
    @ResponseBody
    public List userList() {
        List list = null;
        try {

            TLoginUserExample example = new TLoginUserExample();
            list=loginService.selectByExample(example);
            list = loginService.noTranscation();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }


    @RequestMapping("/getUserList")
    @ResponseBody
    public JSONResult getUserList(PageBean pageBean, TLoginUser loginUser){
        JSONResult result = JSONResult.getSuccess();

        try {
            PageInfo<TLoginUser> pageInfo = loginService.getUserList(pageBean,loginUser);
            result.getBody().put("pageInfo",pageInfo);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.getFail("系统异常！");
        }
    }

    @RequestMapping("/updateUser")
    @ResponseBody
    public JSONResult updateUser(TLoginUser loginUser){
        JSONResult result = JSONResult.getSuccess();
        try {
            TLoginUser loginUser1 = loginService.getById(loginUser.getId());
            loginUser1.setPassword(loginUser.getPassword());
            loginService.updateUser(loginUser1);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return  JSONResult.getFail("系统错误");
        }
    }
    @RequestMapping("/saveUser")
    @ResponseBody
    public  JSONResult saveUser(TLoginUser loginUser){
        JSONResult result = JSONResult.getSuccess();
        try {
            loginUser.setId(UUIDGeneratorUtil.generate().toString());
            loginUser.setCreateTime(matter1.format(new Date()));
            loginService.insertLoginUser(loginUser);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return  JSONResult.getFail("系统错误");
        }
    }
}
